package com.lotter.api.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lotter.common.enumobject.UserAccountApplyEunm;
import com.lotter.common.enumobject.UserAccountStatusEunm;
import com.lotter.common.enumobject.UserAccountTypeEunm;
import com.lotter.common.exception.LotterCusException;
import com.lotter.common.invocation.BaseConditionVO;
import com.lotter.common.util.DateUtil;
import com.lotter.common.util.MD5Util;
import com.lotter.dao.OnlineUserMapper;
import com.lotter.dao.UserAccountMapper;
import com.lotter.invocation.StaticDataContext;
import com.lotter.model.user.OnlineUser;
import com.lotter.model.user.UserAccount;

/**
 * @ClassName: UserAccountService 
 * @Description: 前端用户业务
 * @author zhangzechi
 * @date 2016年7月27日 下午2:06:33  
 * @version V1.0
 */
@Service
public class UserAccountService {
	
	@Autowired
	private UserAccountMapper userAccountMapper ;
	
	@Autowired
	private OnlineUserMapper onlineUserMapper;
	
	/**
	 * 查询房主列表
	 * @param result
	 * @param vo
	 * @return
	 * @throws LotterCusException
	 */
	public Map<String,Object> findUserAccountList(Map<String,Object> result) 
			throws LotterCusException{
		BaseConditionVO vo = new BaseConditionVO();
		vo.setType(UserAccountTypeEunm.PUBLISHER.getCode());//房主
		vo.setStatus(UserAccountStatusEunm.NORMAL.getCode());//正常
		List<UserAccount> list = userAccountMapper.findUserAccountByPage(vo);
		result.put("data", list);
		return result;
	}
	
	
	//手机号码是否已经注册
	public Boolean isExistUserAccountByMobile(String mobile) throws LotterCusException{
		int number = 0 ;
		number = userAccountMapper.conutUserAccountByMobile(mobile);
		if(number > 0)
			return true;
		return false ;
	} 
	
	//登录名是否已经注册
	public Boolean isExistUserAccountByLoginName(String loginname) throws LotterCusException{
		int number = 0 ;
		number = userAccountMapper.conutUserAccountByLoginname(loginname);
		if(number > 0)
			return true;
		return false ;
	} 
	
	/**
	* @Title: findUserAccountByMap 
	* @Description:查询用户信息
	* @param @param mobile
	* @param @param password
	* @param @return
	* @param @throws LotterCusException    设定文件 
	* @return UserAccount    返回类型 
	* @throws
	 */
	public UserAccount findUserAccountByMap(String username,String password) 
			throws LotterCusException{
		Map<String,Object> result = new HashMap<String, Object>();
		result.put("username", username);
		result.put("password", MD5Util.md5Hex(password));
		result.put("ustatus", UserAccountStatusEunm.NORMAL.getCode());
		UserAccount userAccount = null ;
		userAccount = userAccountMapper.findUserAccountByMap(result);
		return userAccount;
	}
	
	
	/**
	* @Title: saveUserAccountByModel 
	* @Description: 保存用户信息
	* @param @param mobile 手机号码
	* @param @param password 密码
	* @param @throws LotterCusException    设定文件 
	* @return void    返回类型 
	* @throws
	 */
	@Transactional(rollbackFor=Exception.class)
	public void saveUserAccountByModel(String mobile,String password,String loginname) 
			throws LotterCusException{
		//装配数据
		UserAccount userAccount = new UserAccount();
		userAccount.setCreatedate(new Date());
		userAccount.setMobile(mobile);
		userAccount.setPassword(MD5Util.md5Hex(password));
		userAccount.setUstatus(UserAccountStatusEunm.NORMAL.getCode());
	    userAccount.setApplyhourse(UserAccountApplyEunm.NOTHING.getCode());
	    userAccount.setUtype(UserAccountTypeEunm.SUBSCRIBER.getCode());
	    userAccount.setLogindate(new Date());
	    userAccount.setLoginname(loginname);
	    userAccount.setLoginnum(1);
	    userAccount.setAmount(0L);
	    userAccount.setScreeningsnum(0);
	    userAccountMapper.insert(userAccount);
	}
	
	
	/**
	* @Title: findloginUserAccount 
	* @Description: 用户登录
	* @param @param mobile
	* @param @param password
	* @param @return
	* @param @throws LotterCusException    设定文件 
	* @return Map<String,Object>    返回类型 
	* @throws
	 */
	@Transactional(rollbackFor=Exception.class)
	public Map<String,Object> updateloginUserAccount(Map<String,Object> result, 
			Long uid,String token,String mobile) 
			throws LotterCusException{
		
		OnlineUser onlineUser=new OnlineUser();
		onlineUser.setUid(uid);
		onlineUser.setToken(MD5Util.md5Hex(token));
		onlineUser.setMobile(mobile);
		onlineUserMapper.deleteByUid(uid);
		onlineUser.setTimeout(DateUtil.getDateplusDay(new Date(), 60, null, null, null));
		onlineUserMapper.insert(onlineUser);
		
		//更新用户登录信息
		userAccountMapper.updateUserAccountByLogin(uid);
		result.put("token", MD5Util.md5Hex(token));
		StaticDataContext.put(onlineUser);
		return result;
	}
	
	/**
	* @Title: updatePwd 
	* @Description: 修改密码
	* @param @param newpassword
	* @param @throws LotterCusException    设定文件 
	* @return Map<String,Object>    返回类型 
	* @throws
	 */
	public void updatePwdByID(String newpassword , Long id) throws LotterCusException{
	    userAccountMapper.updatePwdByID(MD5Util.md5Hex(newpassword), id);
	} 
	
	/**
	* @Title: updatePwd 
	* @Description: 修改密码
	* @param @param newpassword
	* @param @throws LotterCusException    设定文件 
	* @return Map<String,Object>    返回类型 
	* @throws
	 */
	@Transactional(rollbackFor=Exception.class)
	public void updatePwdByMobile(String newpassword , String mobile) throws LotterCusException{
	    userAccountMapper.updatePwdByMobile(MD5Util.md5Hex(newpassword), mobile);
	}
	
	/**
	 * 申请  取消房主申请
	 * @param id
	 * @param type  1 申请  2 取消
	 * @throws LotterCusException
	 */
	@Transactional(rollbackFor=Exception.class)
	public void updateApplyhourse(Long id) throws LotterCusException{
		Integer  applyhourse = UserAccountApplyEunm.APPLY_HOURSE.getCode(); ;
		userAccountMapper.updateApply(applyhourse, id);
	}
	
	
	//根据手机查询用户信息
	public UserAccount findUserByMobile(String mobile){
		return userAccountMapper.findUserByMobile(mobile, UserAccountStatusEunm.NORMAL.getCode());
	}
	
	
	@Transactional(rollbackFor=Exception.class)
	public void updateImgInfo(Long id,String nickname,String headurl) 
			throws LotterCusException{
		UserAccount userAccount = new UserAccount(); 
		userAccount.setId(id);
		userAccount.setHeadurl(headurl);
		userAccount.setNickname(nickname);
		userAccountMapper.updateByPrimaryKeySelective(userAccount);
	}

	//主键查询
	public UserAccount selectByPrimaryKey(Long id) throws LotterCusException{
		return userAccountMapper.selectByPrimaryKey(id);
	}
	
	
	//用户是否存在
	public int countUserByUsername(String username) throws LotterCusException{
		return userAccountMapper.countUserByUsername(username, UserAccountStatusEunm.NORMAL.getCode());
	}
}
